import Decorator from '../core/Decorator';
import { RUNNING } from '../constants';
import Tick from '../core/Tick';
import BaseNode from '../core/BaseNode';
// 超出时间才执行
export default class DelayTime extends Decorator {

  delayTime: number

  constructor(child: BaseNode, delayTime: number) {
    super(`DelayTime(${delayTime}ms)[${child.name}]`, child);
    this.delayTime = delayTime;
  }

  onOpen(tick: Tick, ...args: any[]) {
    var startTime = (new Date()).getTime();
    tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
  }

  onTick(tick: Tick, ...args: any[]) {
    var currTime = (new Date()).getTime();
    var startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
    if (currTime - startTime > this.delayTime) {
      return this.child.execute(tick, ...args);
    }
    return RUNNING;
  }
};
